#include "gui.button.h"

// Constructor
TOB::UIButton::UIButton(uint16 x, uint16 y, uint16 w, uint16 h):
  UIWindow(x, y, w, h) {
}


// Destructor
TOB::UIButton::~UIButton() {
}


// Set focus
void TOB::UIButton::focus() {
  running = true;
  showCursor(false);
  print(true);
  while(running) {
    Char ch = getChar();
    try {
      switch(ch) {
       case '\t':
        eventTab();
        break;

       case '\n':
        running = false;
        eventClick();
        break;

      }
    } catch(std::bad_function_call) {
    }
  }
  print();
}


// Print button
void TOB::UIButton::print(bool selected) {
  setCursor(0, 0);
  setColor((selected) ? Color::ActiveButton : Color::InactiveButton);
  String temp = title;
  if(temp.size() >= width) { 
    temp = temp.substr(0, width - 2); 
  }
  putChar(' ');
  putString(temp);
  for(uint32 i = temp.size() + 1; i < width; ++i) {
    putChar(' ');
  }
}
